package com.zt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zt.mapper.CheckgroupMapper;
import com.zt.pojo.Checkgroup;
import com.zt.pojo.Checkitem;
import com.zt.pojo.vo.CheckgroupVo;
import com.zt.service.CheckgroupService;
import com.zt.service.CheckitemService;
import com.zt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【t_checkgroup】的数据库操作Service实现
* @createDate 2024-11-17 19:36:06
*/
@Service
public class CheckgroupServiceImpl implements CheckgroupService{
    @Autowired
    private CheckgroupMapper checkgroupMapper;

    /**
     * 分页模糊查询
     * @param checkgroupVo
     * @return
     */
    @Override
    public Result findPage(CheckgroupVo checkgroupVo){
        PageHelper.startPage(checkgroupVo.getCurrentPage(), checkgroupVo.getPageSize());
        List<Checkgroup> checkgroupList = checkgroupMapper.findPage(checkgroupVo);
        PageInfo<Checkgroup> checkgroupPageInfo = new PageInfo<>(checkgroupList);
        return Result.success("查询成功",checkgroupPageInfo);
    }

    /**
     * 添加检查组
     * @param checkgroup
     * @return
     */
    @Override
    public void add(Integer[] checkitemIds,Checkgroup checkgroup) {
            checkgroupMapper.add(checkgroup);
            checkgroupMapper.addCheckGroupCheckItem(checkitemIds, checkgroup.getId());
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void delete(Integer id) {
        checkgroupMapper.deleteCheckGroupCheckItemByCheckGroupId( id );
        checkgroupMapper.delete(id);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public Checkgroup findById(Integer id) {
        return checkgroupMapper.findById( id );
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkgroup
     */
    @Override
    public void edit(Integer[] checkitemIds, Checkgroup checkgroup) {
        checkgroupMapper.edit( checkgroup );
        checkgroupMapper.deleteCheckGroupCheckItemByCheckGroupId(checkgroup.getId());
        checkgroupMapper.addCheckGroupCheckItem(checkitemIds,checkgroup.getId());
    }

    /**
     * 回显检查组所对应的检查项id
     * @param id
     * @return
     */
    @Override
    public Integer[] findCheckItemIdsByCheckGroupId(Integer id) {
        return checkgroupMapper.findCheckItemById(id);
    }

    @Override
    public Result findAll() {
        try {
            List<Checkgroup> all = checkgroupMapper.findAll();
            return Result.success("查询成功",all);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("查询失败");
    }
}




